package com.ssm.juc.jmm.demo;

public class SingletonTest {
    //volatile关键字保证可见性 同时禁用指令重排（jdk1.5后生效）
    private static volatile SingletonTest singleton;

    private SingletonTest() {

    }

    /**
     * 双重检查锁实现单例模式
     * 推荐这样写，既保证线程安全，又延迟加载
     *
     * @return
     */
    public static SingletonTest getSingleton() {
        if (singleton == null) {
            synchronized (SingletonTest.class) {
                if (singleton == null) {
                    singleton = new SingletonTest();
                }
            }
        }
        return singleton;
    }
}